package com.wsc.juc_001.c_000;


import java.sql.Time;

public class SleepYieldJoin_03 {
    public static void main(String[] args) {
            //sleep();
            //yield();
            //join();
    }

    private static void join() {
        Thread t1 = new Thread(()->{
            for (int i = 0; i <100 ; i++) {
                System.out.println("t" + i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });


        Thread t2 = new Thread(()->{
            try {
                t1.join();
                System.out.println("t2");
            }catch (Exception e){
                e.printStackTrace();
            }
        });
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.start();

        t2.start();
    }

    private static void yield() {
        new Thread(()->{
            for (int i = 0; i <100 ; i++) {
                System.out.println("a"+i);
                if (i%10 == 0) Thread.yield();
            }
        }).start();

        new Thread(()->{
            for (int i = 0; i <100 ; i++) {
                System.out.println("b"+i);
                if (i%10 == 0) Thread.yield();
            }
        }).start();
    }

    private static void sleep() {
        new Thread(()->{
            for (int i = 0; i <10 ; i++) {
                System.out.println("a"+ i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
/**
 * sleep:cpu暂停
 * yield:让出正在执行的线程
 * join:需要等待其他线程结束完之后才能继续执行  t1,t2 在t2种调用t1.join，说明要等t2执行完之后，t1才开始执行
 * 在t1中调用t2.join，说明要等t1执行完之后，t2才开始执行
 *
 */